#include "functor.h"

namespace core
{



int pixel_functor::operator()( const char * p )
{
    int pixel = 0;
    if( !p || !*p )
    {
        return pixel;
    }

    XmlString str;
    if( strstr( p, "px" ) != 0 )
    {
        p = XmlBase::ReadText( p, &str, true, false, "px", true, XML_DEFAULT_ENCODING );
        if( str.size() != 0 )
        {
            pixel = string2int( str.c_str() );
        }
    }
    else
    {
        pixel = string2int( p );
    }

    return pixel;
}

color color_functor::operator()( const char * p )
{
    color c = 0;
    if( !p || !*p )
    {
        return c;
    }

    const char *q = p;
    if( *q == '#' )
    {
        q++;
        if( strlen(q) == 6 )
        {
            unsigned char r = base::hex2i<unsigned char>( q,     2 );
            unsigned char g = base::hex2i<unsigned char>( q + 2, 2 );
            unsigned char b = base::hex2i<unsigned char>( q + 4, 2 );
            c = (color)( r | ((g << 8) & 0xff00) | ((b << 16) & 0xff0000) );
        }
    }
    else
    {
        XmlString str;
        if( (q = XmlBase::ReadText( q, &str, true, false, "(", true, XML_DEFAULT_ENCODING )) &&
            _strnicmp( str.c_str(), "rgb", 3 ) == 0 )
        {
            XmlString sRed, sGreen, sBlue;

            q = XmlBase::ReadText( q, &sRed, true, false, ",", true, XML_DEFAULT_ENCODING );
            if( sRed.size() == 0 )
                return c;

            q = XmlBase::ReadText( q, &sGreen, true, false, ",", true, XML_DEFAULT_ENCODING );
            if( sGreen.size() == 0 )
                return c;

            q = XmlBase::ReadText( q, &sBlue, true, false, ")", true, XML_DEFAULT_ENCODING );
            if( sBlue.size() == 0 )
                return c;

            unsigned char r = (unsigned char)string2int(sRed.c_str());
            unsigned char g = (unsigned char)string2int(sGreen.c_str());
            unsigned char b = (unsigned char)string2int(sBlue.c_str());

            c = (color)( r | ((g << 8) & 0xff00) | ((b << 16) & 0xff0000) );
        }
    }

    return c;
}

unit unit_functor::operator()( const char * p )
{
    unit u;
    if( !p || !*p )
    {
        return u;
    }

    XmlString str;
    if( strstr( p, "%" ) != 0 )
    {
        p = XmlBase::ReadText( p, &str, true, false, "%", true, XML_DEFAULT_ENCODING );
        if( str.size() != 0 )
        {
            u.use = string2int( str.c_str() );
            u.pixel = false;
        }
    }
    else if( strstr( p, "px" ) != 0 )
    {
        p = XmlBase::ReadText( p, &str, true, false, "px", true, XML_DEFAULT_ENCODING );
        if( str.size() != 0 )
        {
            u.use = string2int( str.c_str() );
            u.pixel = true;
        }
    }
    else
    {
        u.use = string2int( p );
        u.pixel = true;
    }

    return u;
}

std::wstring file_functor::operator()( const char *p )
{
    std::wstring file;
    if( !p || !*p )
    {
        return file;
    }

    XmlString str;
    p = XmlBase::ReadText( p, &str, true, false, "file(", true, XML_DEFAULT_ENCODING );
    if( p && *p )
    {
        p = XmlBase::ReadText( p, &str, true, false, ")", true, XML_DEFAULT_ENCODING );
        if( str.size() != 0 )
        {
            file = base::utf8tounicode(str.c_str());
        }
    }
    return file;
}



}